//
//  CSBDymXqSweeping.swift
//  CurSodoboAer
//
//  Created by CurSodoboAer on 2024/11/5.
//

import UIKit
import JDStatusBarNotification

class CSBDymXqSweeping: UIViewController {
    private let csbBackComButton = UIButton.init()
    private let csbAlertComButton = UIButton.init()
    private let daDymHeadCSB = UIImageView()
    private let uderNamerCSB = UILabel()
    private let uderTimeCSB = UILabel()
   
    private let daDymbgCSB = UIImageView()
    private let uderDymDescCSB = UILabel()
    private let likrCSBButton = UIButton.init()
    private let commentCSBButton = UIButton.init()
    
    private let commrttextContent = UITextField.init()
    
    let bottomCommentView = UIView.init()
    
    private var csbColoter:UIColor = .brown
    private var csbFplowe:CGFloat = 23.1
    private var csbHeihei:UIFont?
    
    
    lazy var emptyCommentView: UIView = {
        let vieewr = UIView.init()
        let logHeadCSB = UIImageView.init(image: UIImage.init(named: "repoihoeder"))
        let noimeCSB = UILabel()
        
        noimeCSB.textColor = .white
        noimeCSB.textAlignment = .center
        noimeCSB.font = UIFont(name: "Helvetica", size: 12)
        noimeCSB.text = "No comments yet"
        logHeadCSB.contentMode = .scaleAspectFit
        vieewr.addSubview(logHeadCSB)
        vieewr.addSubview(noimeCSB)
        logHeadCSB.snp.makeConstraints { make in
            make.width.height.equalTo(80.xQQ)
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(30)
        }
        noimeCSB.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(logHeadCSB.snp.bottom).offset(12)
        }
        return vieewr
    }()
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        bottomCommentView.layer.cornerRadius = 20
        bottomCommentView.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]
        
    }
    
    var dymCSBDataCSB:Dictionary<String,String>
    init(qugaoshCSB:[Int]?,activeDataCSB: Dictionary<String, String>) {
        
        if qugaoshCSB?.first ?? 0 > 0 {
            self.dymCSBDataCSB = activeDataCSB
        }else{
            self.dymCSBDataCSB = activeDataCSB
        }
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func comekwith() {
        
        likrCSBButton.isSelected =   (dymCSBDataCSB["heartForerDym"] ?? "" == "1") ? true : false
        daDymbgCSB.image = UIImage(named: dymCSBDataCSB["csbpersonDymPic"] ?? "")
        daDymHeadCSB.image =  UIImage(named: dymCSBDataCSB["csbpersonHead"] ?? "")
        uderNamerCSB.text =  dymCSBDataCSB["csbpersonName"] ?? ""
        uderTimeCSB.text =   dymCSBDataCSB["csbpersonDymTime"] ?? ""
        uderDymDescCSB.text =   dymCSBDataCSB["csbpersonDymTitle"] ?? ""
        
      
       
    }
    
    var ismeToCSB:Bool = false
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
       
        
        let circliVi = UIView()
        circliVi.backgroundColor = .white
        circliVi.layer.cornerRadius = 5
        circliVi.layer.masksToBounds = true
        comekwith()
        NotificationCenter.default.addObserver(self, selector: #selector(csbButtonClick), name: NSNotification.Name.init("laheiCSBkUsers"), object: nil)
        view.backgroundColor = UIColor(red: 0.64, green: 0.41, blue: 0.98, alpha: 1)
        csbBackComButton.setImage(UIImage.init(named: "csb_back"), for: .normal)
        csbBackComButton.addTarget(self, action: #selector(csbButtonClick), for: .touchUpInside)
        csbAlertComButton.setImage(UIImage.init(named: "active_report"), for: .normal)
        csbAlertComButton.addTarget(self, action: #selector(csbrepotrtClick), for: .touchUpInside)
        
        commrttextContent.layer.cornerRadius = 22
        commrttextContent.layer.masksToBounds = true
        commrttextContent.placeholder = "I'll say a few words too…"
        commrttextContent.textColor = .black
        commrttextContent.backgroundColor = UIColor(red: 0.07, green: 0.07, blue: 0.07, alpha: 0.1200)
        commrttextContent.leftViewMode = .always
        commrttextContent.leftView = UIView(frame: CGRect.init(x: 0, y: 0, width: 30, height: 44))
        
        daDymbgCSB.layer.cornerRadius = 24.xQQ
        daDymbgCSB.layer.masksToBounds = true
        daDymbgCSB.contentMode = .scaleAspectFill

        daDymHeadCSB.isUserInteractionEnabled = true
        daDymHeadCSB.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(csbusercenterClick)))
        daDymHeadCSB.layer.cornerRadius = 22.xQQ
        daDymHeadCSB.layer.masksToBounds = true
        daDymHeadCSB.contentMode = .scaleAspectFill

        uderNamerCSB.textColor = .white
        uderNamerCSB.textAlignment = .left
        uderNamerCSB.font = UIFont(name: "Helvetica Bold", size: 15.xQQ)
        
        
        uderTimeCSB.textColor = .white
        uderTimeCSB.textAlignment = .right
        uderTimeCSB.font = UIFont(name: "Helvetica", size: 12)
        
        uderDymDescCSB.textColor = .white

        uderDymDescCSB.font = UIFont(name: "Kailasa Regular", size: 16)
        
        
        likrCSBButton.addTarget(self, action: #selector(csbgivenheardClick), for: .touchUpInside)
        likrCSBButton.setImage(UIImage.init(named: "dym_detail_likeempty"), for: .normal)
        likrCSBButton.setImage(UIImage.init(named: "dym_detail_like"), for: .selected)
        likrCSBButton.setTitleColor(.white, for: .normal)
        likrCSBButton.titleLabel?.font = UIFont(name: "Kailasa Regular", size: 14)
        
        commentCSBButton.setImage(UIImage.init(named: "dym_detail_shre"), for: .normal)
        commentCSBButton.setTitleColor(.white, for: .normal)
        commentCSBButton.titleLabel?.font = UIFont(name: "Kailasa Regular", size: 14)
        commentCSBButton.addTarget(self, action: #selector(csaddCommentClick), for: .touchUpInside)
        
        view.addSubview(csbBackComButton)
        view.addSubview(csbAlertComButton)
        
        view.addSubview(daDymHeadCSB)
        view.addSubview(uderNamerCSB)
        view.addSubview(uderTimeCSB)
        
        let bavyScroolleview = UIScrollView.init()
        bavyScroolleview.backgroundColor = .clear
        
        view.addSubview(bavyScroolleview)
        view.addSubview(bottomCommentView)
        bottomCommentView.addSubview(self.commrttextContent)
        bottomCommentView.addSubview(self.likrCSBButton)
        bottomCommentView.addSubview(self.commentCSBButton)
        
        
        
        bavyScroolleview.addSubview(daDymbgCSB)
        bavyScroolleview.addSubview(circliVi)
        bavyScroolleview.addSubview(uderDymDescCSB)
        
        
       
        
        
        
        
        
        
        bottomCommentView.backgroundColor = UIColor(red: 0.75, green: 0.99, blue: 0.01, alpha: 1)
        
       
        
        let stattusBarHeight = UIApplication.shared.statusBarFrame.height
        csbBackComButton.snp.makeConstraints { make in
            make.leading.equalToSuperview().inset(12.xQQ)
            make.width.equalTo(24)
            make.height.equalTo(22)
            make.top.equalToSuperview().offset(stattusBarHeight + 15)
        }
        
        csbAlertComButton.snp.makeConstraints { make in
            make.width.height.equalTo(24)
            make.trailing.equalToSuperview().inset(12.xQQ)
            make.centerY.equalTo(csbBackComButton)
        }
        
        daDymHeadCSB.snp.makeConstraints { make in
            make.leading.equalTo(csbBackComButton.snp.trailing).offset(12.xQQ)
            make.width.height.equalTo(44.xQQ)
            make.centerY.equalTo(csbBackComButton)
            
        }
        
        uderNamerCSB.snp.makeConstraints { make in
            make.top.equalTo(daDymHeadCSB.snp.top).offset(7.xQQ)
            make.leading.equalTo(daDymHeadCSB.snp.trailing).offset(13.xQQ)
            make.trailing.equalTo(csbAlertComButton.snp.leading).offset(-14)
        }
        
        uderTimeCSB.snp.makeConstraints { make in
            make.leading.equalTo(uderNamerCSB)
            make.top.equalTo(uderNamerCSB.snp.bottom).offset(4)
        }
        bavyScroolleview.snp.makeConstraints { make in
            make.leading.bottom.trailing.equalToSuperview()
            make.top.equalTo(daDymHeadCSB.snp.bottom).offset(15)
        }
        
        bottomCommentView.snp.makeConstraints { make in
            make.leading.trailing.bottom.equalToSuperview()
            make.height.equalTo(86)
        }
        commrttextContent.snp.makeConstraints { make in
            make.leading.equalToSuperview().inset(12.xQQ)
            make.top.equalToSuperview().offset(6)
            make.width.equalTo(254.xQQ)
            make.height.equalTo(44)
        }
        
        commentCSBButton.snp.makeConstraints { make in
            make.width.height.equalTo(26.xQQ)
            make.centerY.equalTo(commrttextContent)
            make.right.equalToSuperview().offset(-12.xQQ)
        }
        
        
        likrCSBButton.snp.makeConstraints { make in
            make.width.height.equalTo(26.xQQ)
            make.centerY.equalTo(commrttextContent)
            make.right.equalTo(commentCSBButton.snp.left).offset(-24.xQQ)
        }
        
        daDymbgCSB.snp.makeConstraints { make in
            make.leading.trailing.equalToSuperview().inset(8)
            make.top.equalToSuperview()
            make.width.equalTo(UIScreen.main.bounds.width - 16)
            make.height.equalTo(486)
        }
       
        circliVi.snp.makeConstraints { make in
            make.width.height.equalTo(10)
            make.centerX.equalToSuperview()
            make.bottom.equalTo(daDymbgCSB.snp.bottom).offset(-16)
        }
        uderDymDescCSB.numberOfLines = 0
        uderDymDescCSB.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(12)
            make.top.equalTo(daDymbgCSB.snp.bottom).offset(21.xQQ)
        }
        
        
        let linte = UIView.init()
        linte.backgroundColor = UIColor(white: 1, alpha: 0.6)
        bavyScroolleview.addSubview(linte)
        linte.snp.makeConstraints { make in
            make.leading.trailing.equalToSuperview().inset(12)
            make.height.equalTo(1)
            make.top.equalTo(uderDymDescCSB.snp.bottom).offset(24.xQQ)
        }
        
        
        let titicommenNamerCSB = UILabel.init()
        
        titicommenNamerCSB.text = "Comments"
        titicommenNamerCSB.textColor = .white
        titicommenNamerCSB.textAlignment = .left
        titicommenNamerCSB.font = UIFont(name: "Helvetica Bold", size: 15.xQQ)
        bavyScroolleview.addSubview(titicommenNamerCSB)
        titicommenNamerCSB.snp.makeConstraints { make in
            make.leading.equalTo(linte)
            make.top.equalTo(linte.snp.bottom).offset(21)
        }
        bavyScroolleview.addSubview(self.emptyCommentView)
        emptyCommentView.snp.makeConstraints { make in
            make.leading.trailing.equalToSuperview()
            make.height.equalTo(150)
            make.bottom.equalToSuperview().offset(-85 - 30)
            make.top.equalTo(titicommenNamerCSB.snp.bottom).offset(10)
        }
        
        if ismeToCSB == true{
            
            csbAlertComButton.isHidden = true
            
            daDymHeadCSB.isUserInteractionEnabled = false
        }
    }
    
    @objc func csbgivenheardClick()  {
        
        NotificationPresenter.shared.present("")
        NotificationPresenter.shared.displayActivityIndicator(true)
        
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1.0, execute: DispatchWorkItem(block: {
            self.likrCSBButton.isSelected = !self.likrCSBButton.isSelected
            if self.likrCSBButton.isSelected {
                NotificationPresenter.shared.present("Thank you for youe like!", includedStyle: .success)
            }else{
                NotificationPresenter.shared.present("You have canceled like!", includedStyle: .success)
            }
            
            CSBFetcherAll.Hammer.updateIlikeStatus(wight: self.dymCSBDataCSB, isLike: self.likrCSBButton.isSelected)
            return
        }))
       
        
        
    }
    
    @objc func csaddCommentClick()  {
        var csbBayui = [Int]()
        let csbNUmber = Array(repeating: 10, count: 64)
        let csbNumbe2 = Array(repeating: 96, count: 50)
        var cabSunmAll: Int = 0

        csbBayui.append(11)
        cabSunmAll = csbNUmber.count
        if cabSunmAll > 5 {
            csbBayui.append(contentsOf: csbNUmber)
        }else{
            csbBayui.append(contentsOf: csbNumbe2)
        }

        
        if let comentCsb = self.commrttextContent.text,comentCsb.count > 0 {
            NotificationPresenter.shared.present("")
            NotificationPresenter.shared.displayActivityIndicator(true)
            
            DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1.0, execute: DispatchWorkItem(block: {
                
                csbBayui.append(contentsOf: [52, 6, 11])
                csbBayui.removeLast()
                cabSunmAll = csbBayui.count

                if (cabSunmAll >= 0) {
                    self.commrttextContent.text = nil
                }

                if (csbBayui.count >= 0) {
                    NotificationPresenter.shared.present("Your comments will be displayed after approval", includedStyle: .success)
                }
               
                
            }))
           
        }else{
            NotificationPresenter.shared.present("Your comment content cannot be empty", includedStyle: .warning)
  
        }
        
       
        
        
    }
    
    
    @objc func csbButtonClick()  {
        var ikol = ["csbFplowe":csbFplowe]
        if self.view.backgroundColor == .brown {
            ikol["allElseTigerCSB"]  = 77.8
            csbFplowe += 1.1
        }else{
            csbFplowe += 2.1
        }

        if csbFplowe < 10{
            return
        }
        self.navigationController?.popViewController(animated: true)
    }

    @objc func csbrepotrtClick()  {
        var ikol = ["csbFplowe":csbFplowe]
        if self.view.backgroundColor == .brown {
            ikol["allElseTigerCSB"]  = 77.8
            csbFplowe += 1.1
        }else{
            csbFplowe += 2.1
        }

        if csbFplowe < 10{
            return
        }
        self.navigationController?.pushViewController(CSBAIrAlertSweeping.init(), animated: true)
    }
    
    @objc func csbusercenterClick()  {
        var ikol = ["csbFplowe":csbFplowe]
        if self.view.backgroundColor == .brown {
            ikol["allElseTigerCSB"]  = 77.8
            csbFplowe += 1.1
        }else{
            csbFplowe += 2.1
        }

        if csbFplowe < 10{
            return
        }
        self.navigationController?.pushViewController(CSBuservcerSweeping.init(qugaoshCSB: [334,445], alluserDataCSB: dymCSBDataCSB), animated: true)
    }

}
